package com.lishuang.org.controller;


import com.lishuang.basic.util.AjaxResult;
import com.lishuang.basic.util.PageList;
import com.lishuang.org.domain.Employee;
import com.lishuang.org.query.EmployeeQuery;
import com.lishuang.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll() {
        return employeeService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "通过ids批量删除数据")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }

    }


    @PutMapping
    @ApiOperation(value = "添加或者修改(通过有无ID区分)")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "根据店铺ID查询所有员工")
    public List<Employee> list(@PathVariable("shopId") Long shopId) {
        return employeeService.list(shopId);
    }

}
